﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Framework;
using VAdvantage.Controls;
using VAdvantage.Utility;
using VAdvantage.Classes;
using VAdvantage.Model;
using System.Text;
using System.Data.SqlClient;

namespace VAdvantage.Search
{
    public class InfoOrder : Info
    {
        #region Private Variables
        string _queryValue = "";
        int bp;
        //on Amount text change value
        private bool _AmtFromCheck = false;
        private bool _AmtToCheck = false;

        private bool _dateTimeFromCheck = false;
        private bool _dateTimeToCheck = false;
        //  Controls used on form
        private VLabel lblDocumentNo = new VLabel();
        private VTextBox txtDocumentNo = new VTextBox();
        private VLabel lblDescription = new VLabel();
        private VTextBox txtDescription = new VTextBox();
        private VLabel lblPOReference = new VLabel();
        private VTextBox txtPOReference = new VTextBox();
        private VLabel lblBPartner_ID = new VLabel();
        //private MLookup fBPartner_ID;
        private VTextBoxButton vSearchBPartner;
        private VLabel lblDateFrom = new VLabel();
        //private VDateTimePicker.VDateTimePicker dtpDateFrom = new VDateTimePicker.VDateTimePicker();
        private VDatePicker dtpDateFrom = new VDatePicker();
        private VLabel lblDateTo = new VLabel();
        //private VDateTimePicker.VDateTimePicker dtpDateTo = new VDateTimePicker.VDateTimePicker();
        private VAdvantage.Controls.VDatePicker dtpDateTo = new VAdvantage.Controls.VDatePicker();
        private VLabel lblAmtFrom = new VLabel();
        private VNumTextBox txtAmtFrom = new VNumTextBox();
        private VLabel lblAmtTo = new VLabel();
        private VNumTextBox txtAmtTo = new VNumTextBox();
        private VCheckBox chkIsSOTrx = new VCheckBox();


        //Array of Column Info shows in grid     
        private static Info_Column[] _orderLayout = {
		new Info_Column("V", "o.C_Order_ID", typeof(IDColumn)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "C_BPartner_ID").Replace("/"," Or "), "(SELECT Name FROM C_BPartner bp WHERE bp.C_BPartner_ID=o.C_BPartner_ID)", typeof(String)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "DateOrdered"), "o.DateOrdered", typeof(DateTime)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "DocumentNo"), "o.DocumentNo", typeof(String)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "C_Currency_ID"), "(SELECT ISO_Code FROM C_Currency c WHERE c.C_Currency_ID=o.C_Currency_ID)", typeof(String)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "GrandTotal"), "o.GrandTotal",  typeof(Decimal)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "ConvertedAmount"), "currencyBase(o.GrandTotal,o.C_Currency_ID,o.DateAcct, o.AD_Client_ID,o.AD_Org_ID)", typeof(Decimal)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "IsSOTrx"), "o.IsSOTrx", typeof(Boolean)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "Description"), "o.Description", typeof(String)),
		new Info_Column(Msg.Translate(Envs.GetContext(), "POReference"), "o.POReference", typeof(String))
	};

        #endregion

        /// <summary>
        /// Detail Protected Contructor
        /// </summary>
        /// <param name="frame">parent frame</param>
        /// <param name="modal">modal</param>
        /// <param name="windowNo">window no</param>
        /// <param name="value">query value</param>
        /// <param name="multiSelection">multiple selections</param>
        /// <param name="whereClause">where clause</param>
        public InfoOrder(Object frame, bool modal, int windowNo, String value, bool multiSelection, String whereClause)
            : base(frame, modal, windowNo, "o", "C_Order_ID", multiSelection, whereClause)
        {
            base.SetTitle(Msg.GetMsg(Envs.GetContext(), "InfoOrder", true));
            try
            {
                _queryValue = value;
                StatInit();
                _LoadedOK = InitInfo();
            }
            catch (Exception e)
            {
                return;
            }


        }


        /// <summary>
        /// Static Setup - add fields to parameterPanel
        /// </summary>
        private void StatInit()
        {
            Context ctx = Envs.GetContext();
            //Init();
            Lookup _value = null;

            SetBusy(true);

            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                _value = MLookupFactory.Get(Envs.GetContext(), _WindowNo, 3499, DisplayType.Search);
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => InvoKFunction(_value));
            });


            InfoSetting();

            lblDocumentNo.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblDocumentNo.Margin = new Thickness(0, 0, 5, 0);
            lblDescription.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblDescription.Margin = new Thickness(0, 0, 5, 0);
            lblPOReference.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblPOReference.Margin = new Thickness(0, 0, 5, 0);
            lblBPartner_ID.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblBPartner_ID.Margin = new Thickness(0, 0, 5, 0);
            lblDateFrom.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblDateFrom.Margin = new Thickness(0, 0, 5, 0);
            lblDateTo.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblDateTo.Margin = new Thickness(0, 0, 5, 0);
            lblAmtFrom.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblAmtFrom.Margin = new Thickness(0, 0, 5, 0);
            lblAmtTo.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            lblAmtTo.Margin = new Thickness(0, 0, 5, 0);

            //vSearchBPartner goes to invok fuction
            lblDocumentNo.Content = Msg.GetMsg(Envs.GetContext(), "DocumentNo", true);
            lblDescription.Content = Msg.GetMsg(Envs.GetContext(), "Description", true);
            lblPOReference.Content = Msg.GetMsg(Envs.GetContext(), "POReference", true);
            lblBPartner_ID.Content = Msg.GetMsg(Envs.GetContext(), "BPartner", true);
            lblDateFrom.Content = Msg.GetMsg(Envs.GetContext(), "DateOrdered", true);
            lblDateTo.Content = Msg.GetMsg(Envs.GetContext(), "-", true);
            lblAmtFrom.Content = Msg.GetMsg(Envs.GetContext(), "GrandTotal", true);
            lblAmtTo.Content = Msg.GetMsg(Envs.GetContext(), "-", true);

            chkIsSOTrx.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            chkIsSOTrx.Content = Msg.GetMsg(Envs.GetContext(), "Sales Transaction", true);
            //tooltip.SetToolTip(chkIsSOTrx, Msg.GetMsg(Envs.GetContext(), "IsSOTrx", true));
            chkIsSOTrx.IsChecked = true;


            Grid.SetColumn(lblDocumentNo, 0);
            Grid.SetRow(lblDocumentNo, 0);
            this.tablePanel.Children.Add(lblDocumentNo);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            txtDocumentNo.Height = HEIGHT;
            txtDocumentNo.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            Grid.SetColumn(txtDocumentNo, 1);
            Grid.SetRow(txtDocumentNo, 0);
            this.tablePanel.Children.Add(txtDocumentNo);

            Grid.SetColumn(lblBPartner_ID, 2);
            Grid.SetRow(lblBPartner_ID, 0);
            this.tablePanel.Children.Add(lblBPartner_ID);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;




            Grid.SetColumn(chkIsSOTrx, 5);
            Grid.SetRow(chkIsSOTrx, 0);
            this.tablePanel.Children.Add(chkIsSOTrx);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            Grid.SetColumn(lblDescription, 0);
            Grid.SetRow(lblDescription, 1);
            this.tablePanel.Children.Add(lblDescription);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            txtDescription.Height = HEIGHT;
            Grid.SetColumn(txtDescription, 1);
            Grid.SetRow(txtDescription, 1);
            this.tablePanel.Children.Add(txtDescription);


            Grid.SetColumn(lblDateFrom, 2);
            Grid.SetRow(lblDateFrom, 1);
            this.tablePanel.Children.Add(lblDateFrom);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            dtpDateFrom.Height = HEIGHT;
            Grid.SetColumn(dtpDateFrom, 3);
            Grid.SetRow(dtpDateFrom, 1);
            this.tablePanel.Children.Add(dtpDateFrom);


            Grid.SetColumn(lblDateTo, 4);
            Grid.SetRow(lblDateTo, 1);
            this.tablePanel.Children.Add(lblDateTo);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            dtpDateTo.Height = HEIGHT;
            Grid.SetColumn(dtpDateTo, 5);
            Grid.SetRow(dtpDateTo, 1);
            this.tablePanel.Children.Add(dtpDateTo);


            Grid.SetColumn(lblPOReference, 0);
            Grid.SetRow(lblPOReference, 2);
            this.tablePanel.Children.Add(lblPOReference);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            txtPOReference.Height = HEIGHT;
            Grid.SetColumn(txtPOReference, 1);
            Grid.SetRow(txtPOReference, 2);
            this.tablePanel.Children.Add(txtPOReference);

            Grid.SetColumn(lblAmtFrom, 2);
            Grid.SetRow(lblAmtFrom, 2);
            this.tablePanel.Children.Add(lblAmtFrom);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            txtAmtFrom.Height = HEIGHT;
            Grid.SetColumn(txtAmtFrom, 3);
            Grid.SetRow(txtAmtFrom, 2);
            this.tablePanel.Children.Add(txtAmtFrom);


            Grid.SetColumn(lblAmtTo, 4);
            Grid.SetRow(lblAmtTo, 2);
            this.tablePanel.Children.Add(lblAmtTo);
            //this.fieldValue.Dock = System.Windows.Forms.DockStyle.Fill;

            txtAmtTo.Height = HEIGHT;
            Grid.SetColumn(txtAmtTo, 5);
            Grid.SetRow(txtAmtTo, 2);
            this.tablePanel.Children.Add(txtAmtTo);


            txtDocumentNo.KeyDown += new KeyEventHandler(txtDocumentNo_KeyDown);
            txtDescription.KeyDown += new KeyEventHandler(txtDocumentNo_KeyDown);
            txtPOReference.KeyDown += new KeyEventHandler(txtDocumentNo_KeyDown);

            chkIsSOTrx.Checked += new RoutedEventHandler(chkIsSOTrx_Checked);
            chkIsSOTrx.Unchecked += new RoutedEventHandler(chkIsSOTrx_Unchecked);

            dtpDateFrom.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(dtpDateFrom_SelectedDateChanged);
            dtpDateTo.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(dtpDateTo_SelectedDateChanged);

            txtAmtFrom.TextChanged += new TextChangedEventHandler(txtAmtFrom_TextChanged);
            txtAmtTo.TextChanged += new TextChangedEventHandler(txtAmtTo_TextChanged);
            vdgvRecords.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(vdgvRecords_AutoGeneratingColumn);
        }


        /// <summary>
        /// For primary thred to fill lookup and set value in control
        /// </summary>
        /// <param name="_value"></param>
        private void InvoKFunction(Lookup _value)
        {
            vSearchBPartner = new VTextBoxButton("C_BPartner_ID", false, false, true, DisplayType.Search, _value);

            Grid.SetColumn(vSearchBPartner, 3);
            Grid.SetRow(vSearchBPartner, 0);
            this.tablePanel.Children.Add(vSearchBPartner);

            Context ctx = Envs.GetContext();
            //  Set Defaults
            String bp = ctx.GetContext(_WindowNo, "C_BPartner_ID");
            if (bp != null && bp.Length != 0)
            {
                vSearchBPartner.SetValue(bp);
            }

            var count = 0;
            if (vdgvRecords.ItemsSource != null)
            {
                count = vdgvRecords.ItemsSource.OfType<object>().Count();
            }
            base.SetStatusMsg(count.ToString() + " " + Msg.GetMsg(Envs.GetContext(), "SearchRows_EnterQuery", true));

            if (_queryValue != null && _queryValue.Length > 0)
            {
                txtDocumentNo.Text = _queryValue.ToString();
                ExecuteQuery();
            }
            //	Focus
            txtDocumentNo.Focus();
            SetBusy(false);
        }

        void txtAmtTo_TextChanged(object sender, TextChangedEventArgs e)
        {
            _AmtToCheck = true;
        }

        void txtAmtFrom_TextChanged(object sender, TextChangedEventArgs e)
        {
            _AmtFromCheck = true;
        }

        void dtpDateTo_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            _dateTimeToCheck = true;
        }

        void dtpDateFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            _dateTimeFromCheck = true;
        }

        void chkIsSOTrx_Unchecked(object sender, RoutedEventArgs e)
        {
            SetValuesOfControl();
            ExecuteQuery();
        }

        void chkIsSOTrx_Checked(object sender, RoutedEventArgs e)
        {
            SetValuesOfControl();
            ExecuteQuery();
        }

        void txtDocumentNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetValuesOfControl();
                ExecuteQuery();
            }
            else if (e.Key == Key.Down)
            {
                vdgvRecords.Focus();
            }
        }

        /// <summary>
        /// Sets the display setting
        /// </summary>
        protected void InfoSetting()
        {
            RowDefinition row = new RowDefinition();
            row.Height = new GridLength(0, GridUnitType.Auto);
            row.MinHeight = 30;
            this.tablePanel.RowDefinitions.Add(row);

            row = new RowDefinition();
            row.Height = new GridLength(0, GridUnitType.Auto);
            row.MinHeight = 30;
            this.tablePanel.RowDefinitions.Add(row);

            row = new RowDefinition();
            row.Height = new GridLength(0, GridUnitType.Auto);
            row.MinHeight = 30;
            this.tablePanel.RowDefinitions.Add(row);


            ColumnDefinition column = new ColumnDefinition();
            column.Width = new GridLength(30, GridUnitType.Star);
            this.tablePanel.ColumnDefinitions.Add(column);

            column = new ColumnDefinition();
            column.Width = new GridLength(50, GridUnitType.Star);
            this.tablePanel.ColumnDefinitions.Add(column);


            column = new ColumnDefinition();
            column.Width = new GridLength(33, GridUnitType.Star);
            this.tablePanel.ColumnDefinitions.Add(column);

            column = new ColumnDefinition();
            column.Width = new GridLength(50, GridUnitType.Star);
            this.tablePanel.ColumnDefinitions.Add(column);

            column = new ColumnDefinition();
            column.Width = new GridLength(4, GridUnitType.Star);
            this.tablePanel.ColumnDefinitions.Add(column);

            column = new ColumnDefinition();
            column.Width = new GridLength(45, GridUnitType.Star);
            this.tablePanel.ColumnDefinitions.Add(column);
        }


        /// <summary>
        /// General Init
        /// </summary>
        /// <returns>true, if success</returns>
        private bool InitInfo()
        {
            //writen in invock function 
            //Context ctx = Envs.GetContext();
            ////  Set Defaults
            //String bp = ctx.GetContext(_WindowNo, "C_BPartner_ID");
            //if (bp != null && bp.Length != 0)
            //{
            //     vSearchBPartner.SetValue(bp);
            //}

            //  prepare table
            StringBuilder where = new StringBuilder("o.IsActive='Y'");
            if (_whereClause.Length > 0)
            {
                where.Append(" AND ").Append(Util.Replace(_whereClause, "C_Order.", "o."));
            }
            PrepareTable("C_Order o", where.ToString(), "2,3,4");
            return true;
        }


        private void SetValuesOfControl()
        {
            _description = txtDescription.Text;
            _documentNo = txtDocumentNo.Text;
            _POReference = txtPOReference.Text;
            if (dtpDateFrom.Text.Trim() != "")
            {
                dateFrom = Convert.ToDateTime(dtpDateFrom.Text.Trim());
            }
            else
            {
                dateFrom = null;
            }
            if (dtpDateTo.Text.Trim() != "")
            {
                dateTo = Convert.ToDateTime(dtpDateTo.Text.Trim());
            }
            else
            {
                dateTo = null;
            }
            amtFrom = txtAmtFrom.Text;
            amtTo = txtAmtTo.Text;
            isSoTrx = (bool)chkIsSOTrx.IsChecked;
            bpValue = vSearchBPartner.GetValue();
        }

        string _documentNo = "";
        string _description = "";
        string _POReference = "";
        System.DateTime? dateFrom = null;
        System.DateTime? dateTo = null;
        string amtFrom = "";
        string amtTo = "";
        bool isSoTrx = false;
        object bpValue = null;
        /// <summary>
        /// Construct SQL Where Clause and define parameters.
        /// (setParameters needs to set parameters)
        /// Includes first AND
        /// </summary>
        /// <returns>sql</returns>
        public override String GetSQLWhere()
        {
            StringBuilder sql = new StringBuilder();
            if (_documentNo.Length > 0)
            {
                sql.Append(" AND UPPER(o.DocumentNo) LIKE @param1");
            }
            if (_description.Length > 0)
            {
                sql.Append(" AND UPPER(o.Description) LIKE @param2");
            }
            if (_POReference.Length > 0)
            {
                sql.Append(" AND UPPER(o.POReference) LIKE @param3");
            }
            //
            if (bpValue != null && bpValue != DBNull.Value)
            {
                sql.Append(" AND o.C_BPartner_ID=@param4");
            }
            //date check
            if ((dateFrom != null || dateTo != null))
            {
               // if (dateFrom.Value.ToString() != "" || dateTo.Value.ToString() != "")
                {
                    if (!_dateTimeFromCheck && _dateTimeToCheck)
                    {
                        sql.Append(" AND TRUNC(o.DateOrdered,'DD') <= @param6");
                    }
                    else if (_dateTimeFromCheck && !_dateTimeToCheck)
                    {
                        sql.Append(" AND TRUNC(o.DateOrdered,'DD') >= @param5");
                    }
                    else if (_dateTimeFromCheck && _dateTimeToCheck)
                    {
                        sql.Append(" AND TRUNC(o.DateOrdered,'DD') BETWEEN @param5 AND @param6");
                    }
                }
            }
            //Grant total check
            if ((!String.IsNullOrEmpty(amtFrom)) || (!string.IsNullOrEmpty(amtTo)))
            {
                //this is for default text in the number text box
                if (amtFrom == "0" && amtTo == "0")
                {

                }
                else
                {
                    if (!_AmtToCheck && _AmtFromCheck)
                    {
                        sql.Append(" AND o.GrandTotal >= @param7");
                    }
                    else if (!_AmtFromCheck && _AmtToCheck)
                    {
                        sql.Append(" AND o.GrandTotal <= @param8");
                    }
                    else if (_AmtFromCheck && _AmtToCheck)
                    {
                        sql.Append(" AND o.GrandTotal BETWEEN @param7 AND @param8");
                    }
                }
            }
            String chekSelect = isSoTrx == true ? "Y" : "N";
            sql.Append(" AND o.IsSOTrx=" + "'" + chekSelect + "'");

            //log.finer(sql.toString());
            return sql.ToString();
        }


        /// <summary>
        /// Set Parameters for Query.
        /// (as defined in getSQLWhere)
        /// </summary>
        /// <param name="param">statement</param>
        /// <param name="forCount">for counting records</param>
        public override void SetParameters(out SqlParameter[] param, bool forCount)
        {
            String docNo = GetSQLText(_documentNo);
            String des = GetSQLText(_description);
            String orderRef = GetSQLText(_POReference);
            Decimal grantAmtFrom = Util.GetValueOfDecimal(amtFrom);
            Decimal grantAmtTo = Util.GetValueOfDecimal(amtTo);

            if (bpValue != null)
            {
                bp = Util.GetValueOfInt(bpValue);
            }
            else
            {
                bp = 0;
            }

            int paramcount = 0;
            if (!(docNo.Equals("") || docNo.Equals("%")))
            {
                paramcount++;
            }
            if (!(des.Equals("") || des.Equals("%")))
            {
                paramcount++;
            }
            if (!(orderRef.Equals("") || orderRef.Equals("%")))
            {
                paramcount++;
            }
            if (!(bp.Equals("") || bp.Equals("%") || bp == 0))
            {
                paramcount++;
            }
            //becouse this contain default date
            if (_dateTimeFromCheck)
            {
                paramcount++;
            }
            if (_dateTimeToCheck)
            {
                paramcount++;
            }
            if (amtFrom == "0" && amtTo == "0")
            {
            }
            else
            {
                if (_AmtFromCheck)
                {
                    paramcount++;
                }
                if (_AmtToCheck)
                {
                    paramcount++;
                }
            }


            param = new SqlParameter[paramcount];
            int index = 0;
            //	=> Documenet Number
            if (!(docNo.Equals("") || docNo.Equals("%")))
            {
                if (!docNo.EndsWith("%"))
                {
                    docNo += "%";
                }
                param[index++] = new SqlParameter("@param1", docNo);
            }
            //	=> DEscription
            if (!(des.Equals("") || des.Equals("%")))
            {
                if (!des.EndsWith("%"))
                {
                    des += "%";
                }
                param[index++] = new SqlParameter("@param2", des);
            }
            //	=> Order Refrence
            if (!(orderRef.Equals("") || orderRef.Equals("%")))
            {
                if (!orderRef.EndsWith("%"))
                {
                    orderRef += "%";
                }
                param[index++] = new SqlParameter("@param3", orderRef);
            }
            //	=> Busness Partner
            if (!(bp.Equals("") || bp.Equals("%") || bp == 0))
            {
                param[index++] = new SqlParameter("@param4", bp);
                log.Fine("BPartner=" + bp);
            }
            if ((dateFrom != null || dateTo != null))
            {
               // if (dateFrom.Value.ToString() != "" || dateTo.Value.ToString() != "")
                {
                    //	=> Date Ordered From
                    if (_dateTimeFromCheck)
                    {
                        param[index++] = new SqlParameter("@param5", dateFrom.Value.ToString("dd-MMM-yy"));
                    }
                    //	=> Date Ordered TO
                    if (_dateTimeToCheck)
                    {
                        param[index++] = new SqlParameter("@param6", dateTo.Value.ToString("dd-MMM-yy"));
                    }
                }
            }
            if (amtFrom == "0" && amtTo == "0")
            {
            }
            else
            {
                //	=> amount from
                if (_AmtFromCheck)
                {
                    param[index++] = new SqlParameter("@param7", grantAmtFrom);
                }
                //	=> Date Amt to
                if (_AmtToCheck)
                {
                    param[index++] = new SqlParameter("@param8", grantAmtTo);
                }
            }
        }


        /// <summary>
        /// Get SQL WHERE parameter
        /// </summary>
        /// <param name="f">field</param>
        /// <returns>sql</returns>
        private String GetSQLText(VTextBox f)
        {
            String s = f.Text.ToUpper();
            if (!s.EndsWith("%"))
            {
                s += "%";
            }
            //log.fine("String=" + s);
            return s;
        }

        /// <summary>
        /// Get SQL WHERE parameter
        /// </summary>
        /// <param name="f">field</param>
        /// <returns>sql</returns>
        private String GetSQLText(string f)
        {
            String s = f.ToUpper();
            if (!s.EndsWith("%"))
            {
                s += "%";
            }
            //log.fine("String=" + s);
            return s;
        }


        /// <summary>
        /// Get Id
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        private int GetAD_Window_ID(String tableName, Query query)
        {
            return (ZoomTarget.GetZoomAD_Window_ID(tableName, 0, query.GetWhereClause(), true));
        }


        /// <summary>
        /// Zoom
        /// </summary>
        public void Zoom()
        {
            log.Info("");
            int? C_Order_ID = GetSelectedRowKey();
            if (C_Order_ID == null)
            {
                return;
            }
            Query query = new Query("C_Order");
            query.AddRestriction("C_Order_ID", Query.EQUAL, C_Order_ID);
            query.SetRecordCount(1);
            int AD_WindowNo = GetAD_Window_ID("C_Order", query);
            //Zoom(AD_WindowNo, query);
        }


        /// <summary>
        /// Has Zoom
        /// </summary>
        /// <returns>true</returns>
        bool HasZoom()
        {
            return true;
        }


        /// <summary>
        /// Get Layout
        /// </summary>
        /// <returns>array of Column_Info</returns>
        protected override Info_Column[] GetInfoColumns()
        {
            return _orderLayout;
        }


        protected override void bRefresh_Click(object sender, RoutedEventArgs e)
        {
            SetValuesOfControl();
            ExecuteQuery();
        }

        void vdgvRecords_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            foreach (DataGridColumn col in vdgvRecords.Columns)
            {
                if (col.DisplayIndex == 0)
                {
                    col.Visibility = Visibility.Collapsed;
                }
            }
        }

    }
}
